//
//  CustomTextField.m
//  FreshMember
//
//  Created by qyc on 2019/4/28.
//  Copyright © 2019年 qyc. All rights reserved.
//

#import "CustomTextField.h"
#import "CheckHelper.h"
@implementation CustomTextField

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        [self addTarget:self
                 action:@selector(textFiledEditChanged:)
       forControlEvents:UIControlEventEditingChanged];
    }
    return self;
}

-(void)awakeFromNib
{
    [super awakeFromNib];
    if (self) {
        
        if (_textLength == 0) {
            _textLength = 10000;
        }
        
        [self addTarget:self
                 action:@selector(textFiledEditChanged:)
       forControlEvents:UIControlEventEditingChanged];
    }
    
}

-(void)textFiledEditChanged:(id)sender {
    UITextRange * selectedRange = self.markedTextRange;
    if(selectedRange == nil || selectedRange.empty){
        NSString *inPutString = [NSString stringWithFormat:@"%@", self.text];
        
        if (self.isHaveEmoji) {
            
        } else if ([CheckHelper stringContainsEmoji:inPutString]) {
            NSInteger largeTo = inPutString.length - 2;
            if (largeTo >= 0) {
                self.text = [inPutString substringToIndex:self.text.length - 2];
//                [YDProgressHUD showMessage:@"不支持表情输入" time:1];
                
                if (largeTo==1) {
                    self.text = @"";
                }
                return;
            }
            return;
        }
        
        NSInteger currentLength = inPutString.length;
        
        if (currentLength > self.textLength) {
            inPutString=[inPutString substringWithRange:NSMakeRange(0, self.textLength)];
            self.text=inPutString;
        }
        
        
    }
}

/**
 获取当前输入长度（汉字-2 英文-1）
 */
- (NSInteger)getStringLength:(NSString*)strTemp {
    NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
    NSData* da = [strTemp dataUsingEncoding:enc];
    return [da length];
}

- (void)setTextLength:(NSInteger)textLength {
    if (_textLength != textLength) {
        _textLength = textLength;
    }
}

@end
